package com.kly.exception;

import com.kly.common.ErrorCode;
import com.kly.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author admin
 * @date 2025/2/17
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public Result<String> runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException: {}", e.getMessage());
        return Result.error(ErrorCode.SYSTEM_ERROR);
    }

    @ExceptionHandler(BusinessException.class)
    public Result<String> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException: {}{}", e, e.getMessage());
        return Result.error(e.getCode(), e.getMessage(), e.getDescription());
    }
}
